// UserManagementController.java
package com.baozheyangken.ai_learn.controller;

import com.baozheyangken.ai_learn.annotation.RequiresAuthentication;
import com.baozheyangken.ai_learn.pojo.entity.User;
import com.baozheyangken.ai_learn.pojo.result.Result;
import com.baozheyangken.ai_learn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
@RequiresAuthentication
public class UserManagementController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     * @return 用户列表
     */
    @GetMapping
    public ResponseEntity<Map<String,Object>> getUsers() {
        List<User> users = userService.getAllUsers();
        Map<String,Object> map = new HashMap<>();
        map.put("data", users);
        map.put("code", 1);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /**
     * 更新用户信息
     * @param id 用户ID
     * @param user 用户信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public Result<User> updateUser(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        User updatedUser = userService.updateUser(user);
        return Result.success(updatedUser);
    }

    /**
     * 重置用户密码
     * @param id 用户ID
     * @return 操作结果
     */
    @PostMapping("/{id}/reset-password")
    public Result resetPassword(@PathVariable Integer id) {
        userService.resetPassword(id);
        return Result.success();
    }
}